package com.DHH.Controller.SysSetting;

import com.DHH.Config.ApiConstant;
import com.DHH.Dto.Message;
import com.DHH.Dto.SysSetting;
import com.DHH.Entity.Base.BaseUserInfo;
import com.DHH.Entity.WxOrder.WxOrderInfo;
import com.DHH.Mapper.Base.BaseUserInfoMapper;
import com.DHH.Mapper.WxOrder.WxOrderInfoMapper;
import com.DHH.wx.action.Pay_Refund;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * Created by 汪玉东 on 2017/9/11.
 */
@Controller
@RequestMapping("sys/setting")
public class SysSettingController {
    @Autowired
    private WxOrderInfoMapper wxOrderInfoMapper;
    @Autowired
    private BaseUserInfoMapper baseUserInfoMapper;
    @RequestMapping("/")
    public String viewPage(ModelMap modelMap){
        SysSetting sysSetting= ApiConstant.getSysSetting();
        modelMap.put("sysData",sysSetting);
        return  "/admin/sysSetting/detailOrderInfoPage.jsp";
    }
    @RequestMapping("/save")
    public String save(SysSetting sysSetting,ModelMap modelMap){
        ApiConstant.setSysSetting(sysSetting);
        return  viewPage(modelMap);
    }

    @RequestMapping("/wxPayReFu")
    public String viewPayPage(){
        return  "/admin/sysSetting/wxPayReFu.jsp";
    }

    @RequestMapping("/wxPayReFuPost")
    @ResponseBody
    public Message wxPayReFuPost(String wxOrder,String wxOrderMoney,String wxMoney){
        Message message=new Message();


        WxOrderInfo wxOrderInfo=new WxOrderInfo();
        wxOrderInfo.setWxorderId(wxOrder);
        wxOrderInfo=wxOrderInfoMapper.selectOne(wxOrderInfo);
        String reMondy=wxOrderInfo.getRemoney();
        if(reMondy==null||reMondy.equals("")){
            reMondy="0";
        }
        Integer cM=Integer.parseInt(reMondy)+Integer.parseInt(wxMoney);

        if(cM<=Integer.parseInt(wxOrderInfo.getMoney())){
               Pay_Refund pay_launch=new Pay_Refund();
               Map map=pay_launch.postMap(wxOrder,wxOrderMoney,wxMoney);

               if( map.get("result_code")!=null&&map.get("result_code").equals("SUCCESS")){
                   wxOrderInfo.setRemoney(cM.toString());
                   wxOrderInfoMapper.updateByPrimaryKeySelective(wxOrderInfo);

                   BaseUserInfo baseUserInfo=new BaseUserInfo();
                   baseUserInfo.setUserId(wxOrderInfo.getUserId());
                   baseUserInfo=baseUserInfoMapper.selectOne(baseUserInfo);

                   Float wxmf=Float.parseFloat(wxMoney)/100;
                   baseUserInfo.setSurplusMoney(baseUserInfo.getSurplusMoney()-wxmf);

                   baseUserInfoMapper.updateByPrimaryKeySelective(baseUserInfo);

                   message.setState(true);
                   message.setMsg("操作成功");
                   return  message;
               }
        }else{
            message.setState(false);
            message.setMsg("操作失败");
            return  message;
        }

        message.setState(false);
        message.setMsg("操作失败");
        return  message;

    }


}
